import router from '@/router'
import {post} from '@/util/request'

export default {
	namespace:'baseinfoVehicle',
	namespaced:true,
	state:{
		isLoading:false,
		isModalLoading:false,
    pageData:{},
		viewData:{}
	},
	mutations:{
		changeLoading(state,v){
			state.isLoading = v;
		},
		changeModalLoading(state,v){
			state.isModalLoading = v;
		},
    changePagedata(state,v){
      state.pageData = v;
    },
		changeViewData(state,v){
			state.viewData = v;
		}
	},
	actions:{
    async reload({commit},query){
      commit('changeLoading',true);
      let res = await post('/base/vehicle/vehicleList',query);
      commit('changePagedata',res.data.data);
      commit('changeLoading',false);
    },
		async loadView({commit},id){
			commit('changeModalLoading',true);
			let res = await post('/base/vehicle/findVehicleById',{id});
			commit('changeViewData',res.data.data);
			commit('changeModalLoading',false);
		},
		delete({commit},{id}){
			commit('changeModalLoading',true);
			return post('/base/vehicle/deleteVehicle',{id}).then(data=>{
				commit('changeModalLoading',false);
				return data.data;
			})
		},
	},
	subscriptions({dispatch},router){
		//给路由加监听一旦进入module绑定的页面时dispatch初始化action
		router.afterEach((to,from)=>{
			if(to.name === this.namespace){
				dispatch('reload',{})
			}
		})
	}
}
